<?php
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
*
*/
class User_profile extends CI_Controller
{

    var $id;
    var $phoneno;
    

    function  __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        
        $this->load->model('Customer');
       // 
      //  echo $id;
        $this->phoneno = $this->session->userdata('phoneno');

    }
     function entrance()
    {

        if($this->session->userdata('logged_in')!=null && $this->session->userdata('rank')=='customer' )
        {
            $this->phoneno = $this->session->userdata('phoneno');
            return 1;
        }
        else
        {
            redirect('home');
        }
    }
    function index()
    {
         $this->entrance();
         $data["title"]="User Profile";
         $data["content"]="user_profile/content";
         $data["sidebar"]="user_profile/sidebar";
         
         $js[]="user_profile.js";
         $js[]="jquery.form.js";
         $data["js"]=$js;
        // print_r($data["image"]);
         $data["profile"]=$this->Customer->get_profile($this->phoneno);
         if($data["profile"]["image"]==null)
             $data["profile"]["image"]="face.jpg";
         
           $this->id= $data["profile"]["Id"] ;
         $this->load->model('Shiftconnection');
         $this->load->model('Complain');

         $data["shift"] = $this->Shiftconnection->get_appid($this->phoneno);
         $data["com"] = $this->Complain->get_appid($this->phoneno);
        // if($data["shift"]->appid==null)
        //     echo "You haven't applied for shift connection yet";
         //print_r($data["profile"]);
         $this->load->view("template",$data);

    }
    function upload_image()
    {
       $this->entrance();
       $config["upload_path"]='./user_images/';
       $config['allowed_types']='gif|jpg|png|bmp';
       $config['file_name']='image_'.$this->id;
       $config['max_size']= '500';

    //   $config['overwrite']=true;
      // $config['max_width']= '200';
      // $config['max_height']= '200';
       $this->load->library('upload',$config);
       $this->load->model('Telephone');
       $aid = $this->Telephone->get_id($this->phoneno);
       if ( ! $this->upload->do_upload())
       {
            echo 'ERROR! '.$this->upload->display_errors('','');

       }
       else
       {

           $data=$this->upload->data();
           
           $this->Customer->update_image($data['file_name'],$aid);
          // echo $id;
           echo $data['file_name'];
       }
        
    }
    function shift_ajax()
    {
        $this->load->model('Shiftconnection');
      //  $shift_appid = $this->Shiftconnection->get_appid($this->phoneno);
        $appid=$_POST['appid'];
       // print_r($appid);
       // $com_appid = $this->Complain->get_appid($this->phoneno);

        echo json_encode($this->Shiftconnection->shift_message($appid));
    }
}
